package cn.jd.org.controller;

import cn.jd.basic.anno.LogAnno;
import cn.jd.basic.anno.PreAuthorize;
import cn.jd.basic.config.BussinessException;
import cn.jd.basic.util.AjaxResult;
import cn.jd.basic.util.PageList;
import cn.jd.org.domain.Department;
import cn.jd.org.domain.Employee;
import cn.jd.org.query.DepartmentQuery;
import cn.jd.org.service.DepartmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/dept")
@Api(value = "部门的API",description="部门相关的CRUD功能")
@LogAnno
@PreAuthorize
public class DepartmentController {
    @Autowired
    private DepartmentService departmentService;
    @PutMapping
    @PreAuthorize(name = "部门添加和修改数据",sn = "department:addDepartment")
    public AjaxResult addDepartment(@RequestBody Department department) {
        try {
            if(department.getId() == null){
                departmentService.save(department);
                return AjaxResult.me().success();
            }else {
                departmentService.update(department);
                return AjaxResult.me().success();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return AjaxResult.me().fail();
    }
/*    @DeleteMapping("/{id}")
    @LogAnno(method = "deleteDepartment", methodDes = "删除的方法")
    public  AjaxResult deleteDepartment(@PathVariable("id") Long id){

        try {
            departmentService.remove(id);
            return AjaxResult.me().success();
        }catch (BussinessException bu){
            bu.printStackTrace();
            return AjaxResult.me().fail(bu.getMessage());
        }
        catch (Exception e){
            e.printStackTrace();
        }
        return AjaxResult.me().fail();
    }*/
    @DeleteMapping("/{id}")
    @LogAnno(method = "deleteDepartment", methodDes = "删除的方法")
    @PreAuthorize(name = "部门根据id删除数据",sn = "department:delete")
    public  AjaxResult deleteDepartment(@PathVariable("id") Long id){

        try {
            departmentService.removeAll(id);
            return AjaxResult.me().success();
        }catch (BussinessException bu){
            bu.printStackTrace();
            return AjaxResult.me().fail(bu.getMessage());
        }
        catch (Exception e){
            e.printStackTrace();
        }
        return AjaxResult.me().fail();
    }
    @GetMapping
    @ApiOperation(value = "查询所有数据" )
    @PreAuthorize(name = "部门查询所有数据",sn = "department:findall")
    public List<Department> findall(){
        return departmentService.findAll();
    }
    @GetMapping("/{id}")
    @ApiOperation(value = "根据id查询一条数据" )
    @PreAuthorize(name = "部门根据id查询一条数据",sn = "department:findById")
    public  Department findall(@ApiParam(value="传入long类型的数据",required = true) @PathVariable("id") Long id){
        return departmentService.findById(id);
    }
    @PostMapping
    @LogAnno(method = "findall", methodDes = "分页查询的方法")
    @PreAuthorize(name = "部门分页查询高级搜索",sn = "department:queryByPage")
    public PageList<Department> findall(@RequestBody DepartmentQuery query){

        return departmentService.queryByPage(query);
    }
    // 批量删除
    @PatchMapping
    @PreAuthorize(name = "部门批量删除",sn = "department:deleteByAll")
    public  AjaxResult deleteAll(@RequestBody List<Long> ids){

        try {
            departmentService.deleteByAll(ids);
            return AjaxResult.me().success();
        }catch (Exception e){
            e.printStackTrace();
        }
        return AjaxResult.me().fail();
    }
    // 无线级数
    @GetMapping("/tree")
    @PreAuthorize(name = "部门无线级树",sn = "department:findDeptree")
    public List<Department> findDeptTree(){
        return departmentService.findDeptree();
    }
    // 抽屉
    @GetMapping("/deptemployee/{id}")
    @PreAuthorize(name = "抽屉",sn = "department:findDeptdeptemployee")
    public List<Employee> findDeptdeptemployee(@PathVariable("id") Long id){

        List<Employee> list = departmentService.findDeptdeptemployee(id);
        System.out.println(list);
        return list;
    }
}
